#!/usr/bin/python
'''
Sorts a HAM file by name of by location.
'''
import sys
import ham
import h5py
import time
import logging
import numpy as np

# parameters.
ham_file = sys.argv[1]
sort_type = sys.argv[2]

# set logging.
logging.basicConfig(level=logging.DEBUG, format='[%(levelname)s] (%(threadName)-10s) %(message)s', )

# open ham pointer.
ham_obj = ham.HamFile(ham_file, read=2)

# choose sorting type.
if sort_type == "name":
	# read entire structure into memory.
	logging.debug("loading ham file into memory.")
	tstart = time.time()
	ham_arr = ham_obj.load_complete()
	#ham_arr = ham_obj.data[0:10]
	
	logging.debug("took %f seconds." % (time.time() - tstart))
	
	# sort the structure.
	logging.debug("sorting array in memory.")
	tstart = time.time()
	ham_arr.sort(axis=0, order='QNAME')
	logging.debug("took %f seconds." % (time.time() - tstart))
	
else:
	print "not implemented."
	sys.exit(1)


# save back to disk.
logging.debug("saving array to disk.")
tstart = time.time()
ham_obj.save_complete(ham_arr)
ham_obj.close()
logging.debug("took %f seconds." % (time.time() - tstart))

